package com.bluedot.www.core.service;

import com.bluedot.www.core.dao.Execute;
import com.bluedot.www.core.pojo.DO.Right;
import com.bluedot.www.core.pojo.DO.Role;
import com.bluedot.www.core.pojo.DTO.RightJudgeDTO;
import com.bluedot.www.core.utils.DbUtil;
import com.bluedot.www.framework.mvc.annotation.MyService;
import com.sun.mail.imap.Rights;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author DengHongWei
 * @since 2021-08-07
 */
@MyService
public class RightService {

    private static final String RIGHT_MAPPER_ADDRESS = "com.bluedot.www.core.mapper.xml.RightMapper.";

    private static final String ROLE_MAPPER_ADDRESS = "com.bluedot.www.core.mapper.xml.RoleMapper.";

    private static final String User_MAPPER_ADDRESS = "com.bluedot.www.core.mapper.xml.UserMapper.";

    public static Object execute(String methodName, Object[] param) {
        RightService rightService = new RightService();
        Object result = null;
        switch (methodName) {
            case "updateStatusById":
                result = rightService.updateStatusById((Long) param[0], (Long) param[1], (Integer) param[2]);
                break;
            case "insertRight":
                result = rightService.insertRight((Right) param[0]);
                break;
            case "deleteRightByRightId":
                result = rightService.deleteRightByRightId((List<Long>) param[0]);
                break;
            case "judgeUserCanAccess":
                result = rightService.judgeUserCanAccess((RightJudgeDTO) param[0]);
                break;
            case "findRightByUrl":
                result = rightService.findRightByUrl((String) param[0]);
                break;
            default:
                throw new RuntimeException("Method NOT FOUND, method name: " + methodName);
        }
        return result;
    }


    //------------------------ZhouXuan---------------------------------

    /**
     * 根据url得到所在的权限
     *
     * @param reqPath:
     * @return com.bluedot.www.core.pojo.DO.Right
     * @author ZhouXuan
     * @date 2021/9/8 15:30
     */
    private Right findRightByUrl(String reqPath) {
        List<Right> rights = (List<Right>) Execute.execute(Execute.QUERY_TYPE, RIGHT_MAPPER_ADDRESS + "findRightByUrl", new Object[]{reqPath});
        if (rights.size() == 0) {
            return null;
        } else {
            return rights.get(0);
        }
    }


    /**
     * 判断用户是否用户权限
     *
     * @return boolean
     * @author ZhouXuan
     * @date 2021/9/2 13:59
     */
    private boolean judgeUserCanAccess(RightJudgeDTO rightJudgeDTO) {
        // 查询该权限
        List<Right> rights = (List<Right>) Execute.execute(Execute.QUERY_TYPE, RIGHT_MAPPER_ADDRESS + "findRightByUrl", new Object[]{rightJudgeDTO.getReqPath()});
        boolean flag = false;
        Right right = null;
        if (rights.size() > 0) {
            right = rights.get(0);
            List<Role> roles1 = (List<Role>) Execute.execute(Execute.QUERY_TYPE, ROLE_MAPPER_ADDRESS + "findByRightId", new Object[]{right.getId()});
            List<Role> roles2 = rightJudgeDTO.getUserDTO().getExistRoles();

            for (Role role1 : roles1) {
                for (Role role2 : roles2) {
                    if (role1.getId() == role2.getId()) {
                        flag = true;
                    }
                }
            }
        }
        return flag;
    }


    /**
     * 根据权限实体类，添加、插入一条权限
     *
     * @param right:
     * @return boolean
     * @author ZhouXuan
     * @date 2021/9/4 14:22
     */
    private boolean insertRight(Right right) {
        // 查找该权限是否存在
        List<Rights> rightList = (List<Rights>) Execute.execute(Execute.QUERY_TYPE, RIGHT_MAPPER_ADDRESS + "findRightByNameOrUrl", new Object[]{right});

        // 如果名称或者URL都不存在，才可以进行查询
        if (rightList.size() == 0) {
            // 设置主键
            right.setId(DbUtil.generateId());

            // 设置时间
            right.setGmtCreate(DbUtil.getCurrentTime());
            right.setGmtModified(DbUtil.getCurrentTime());

            Integer count = (Integer) Execute.execute(Execute.UPDATE_TYPE, RIGHT_MAPPER_ADDRESS + "insertRight", new Object[]{right});
            return count > 0;
        }
        return false;
    }

    /**
     * 根据权限id，删除多条权限
     *
     * @param rightIds:
     * @return boolean
     * @author ZhouXuan
     * @date 2021/9/4 14:38
     */
    private boolean deleteRightByRightId(List<Long> rightIds) {
        // 1.删除权限和角色的中间表的数据
        for (Long rightId : rightIds) {
            Integer count = (Integer) Execute.execute(Execute.UPDATE_TYPE, RIGHT_MAPPER_ADDRESS + "deleteRoleRightByRightId", new Object[]{rightId});
        }

        // 2.删除权限表中的数据
        Integer sum = 0;
        Integer count;
        for (Long rightId : rightIds) {
            count = (Integer) Execute.execute(Execute.UPDATE_TYPE, RIGHT_MAPPER_ADDRESS + "deleteRightById", new Object[]{rightId});
            sum += count;
        }

        // 如果删除的结果数和权限列表的条数相同，则删除成功
        if (rightIds.size() == sum) {
            return true;
        }
        return false;
    }


    /**
     * 用于更新用户或权限或角色的封禁状态
     *
     * @param id:     用户或权限或角色的id编号
     * @param status: 指要改变的状态值 1开启 0关闭
     * @param type:   指用于判断是用户还是角色还是权限：user/role/right-->0/1/2
     * @return boolean
     * @author ZhouXuan
     * @date 2021/9/4 14:49
     */
    private boolean updateStatusById(Long id, Long status, Integer type) {
        boolean flag = false;
        Map<String, String> map1 = new HashMap<>();
        Map<String, String> map2 = new HashMap<>();
        map1.put("id", id.toString());
        map2.put("status", status.toString());

        // 通过type判断要封禁的对象
        switch (type) {
            case 0:
                Integer count1 = (Integer) Execute.execute(Execute.UPDATE_TYPE, User_MAPPER_ADDRESS + "updateUserStatusById", new Object[]{map1, map2});
                flag = count1 > 0;
                break;
            case 1:
                Integer count2 = (Integer) Execute.execute(Execute.UPDATE_TYPE, ROLE_MAPPER_ADDRESS + "updateRoleStatusById", new Object[]{map1, map2});
                flag = count2 > 0;
                break;
            case 2:
                Integer count3 = (Integer) Execute.execute(Execute.UPDATE_TYPE, RIGHT_MAPPER_ADDRESS + "updateRightStatusById", new Object[]{map1, map2});
                flag = count3 > 0;
                break;
        }
        return flag;
    }
    //------------------------ZhouXuan---------------------------------
}



